/*
 * GrafoPanel.java
 *
 * 
 */

package it.luigiarpino.alt2jsim.netpanel.flat;

import it.luigiarpino.alt2jsim.network.Net;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Color;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class GrafoPanel extends JPanel{
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -2710887670791375695L;
	private Net net;
    private Rectangle r;

    /** Visualizza la vista in scala della rete */
    public GrafoPanel(Net net_) {
        super();
        try { 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
        this.net=net_;
        this.setToolTipText("H");
    }

    
    public String getToolTipText(java.awt.event.MouseEvent evt){
        int x=evt.getX();
        int y=evt.getY();
        int offsetX=(int)(r.width*0.05);
        int offsetY=(int)(r.height*0.05);
        int halfWidthNode=(int)(r.width/400);
        int halfHeightNode=(int)(r.height/400);
        int num=-1;
        //verifico se l'utente clicca su di un nodo
        for(int i=0;i<this.net.posizioneNodi.length;i++){
            float xx=this.net.posizioneNodi[i][0];
            float yy=this.net.posizioneNodi[i][1];
            int x1=(int)((xx/this.net.scale)*r.width*0.9);
            int y1=(int)((yy/this.net.scale)*r.height*0.9);
            x1=x1+offsetX;
            y1=y1+offsetY;
            if((x<x1+halfWidthNode)&&(x>x1-halfWidthNode)&&(y<y1+halfHeightNode)&&(y>y1-halfHeightNode)){
                num=i;
            }
        }
        // se l'utente ha cliccato su di un nodo
        String suggerimento=new String();
        if (num>-1){
           suggerimento="<HTML><H3>Node: "+
           this.net.nomiNodi[num]+"</H3></HTML>";
           return suggerimento;
       }
       else{
           suggerimento="<HTML><H3>Information:</H3>"+
           "Place your cursor on a node<BR>" +
           "to know its name</HTML>";
           return suggerimento;
       }
    }
    
    
    public void paintComponent(Graphics g) {
        this.r=new Rectangle(this.getBounds());
        r.height=this.getHeight();
        r.width=this.getWidth();
        g.clearRect(0,0,r.width ,r.height);
        g.setColor(Color.BLACK);
        int offsetX=(int)(r.width*0.05);
        int offsetY=(int)(r.height*0.05);
        for(int i=0;i<this.net.numOfArch;i++){
            int idNodoSource=this.net.archi[i][0];
            int idNodoDest=this.net.archi[i][1];
            float xx1=this.net.posizioneNodi[idNodoSource][0];
            float yy1=this.net.posizioneNodi[idNodoSource][1];
            float xx2=this.net.posizioneNodi[idNodoDest][0];
            float yy2=this.net.posizioneNodi[idNodoDest][1];
            int x1=(int)((xx1/this.net.scale)*r.width*0.9);
            int y1=(int)((yy1/this.net.scale)*r.height*0.9);
            int x2=(int)((xx2/this.net.scale)*r.width*0.9);
            int y2=(int)((yy2/this.net.scale)*r.height*0.9);
            g.setColor(Color.RED);
            g.drawLine(x1+offsetX, y1+offsetY, x2+offsetX, y2+offsetY);
            g.setColor(Color.BLACK);
            int widthNode=(int)(r.width/200);
            int heightNode=(int)(r.height/200);
            int halfWidthNode=(int)(r.width/400);
            int halfHeightNode=(int)(r.height/400);
            g.fillRect(x1+offsetX-halfWidthNode, y1+offsetY-halfHeightNode,widthNode,heightNode);
            g.fillRect(x2+offsetX-halfWidthNode, y2+offsetY-halfHeightNode,widthNode,heightNode);
        }// fine for
    }// fine paintComponent()
       
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
    
}
